<?php

namespace App\Http\Requests\Api;

use App\Http\Requests\CommonRequest;

class UserRequest extends CommonRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {

        $rules = [
            'phone' => 'required|regex:/^1[345789][0-9]{9}$/',
            'password' => 'required',
            'c_password' => 'required|same:password',
        ];

        switch($this->scene())
        {
            case 'register':
            {
                return $rules;
            }
            case 'login':
            {
                return array_except($rules,['c_password']);
            }
            case 'repass':
            {
                return [
                    'oldpassword' => 'required',
                    'password' => 'required',
                    'c_password' => 'required|same:password'
                ];
            }
            case 'headimg':
            {
                return [];
            }
            case 'update':
            {
                return [];
            }
            case 'search':
            {
                return [];
            }
            case 'ban':
            {
                return [];
            }
            case 'create':
            {
                return [
                    'phone' => 'required|regex:/^1[345789][0-9]{9}$/',
                    'role_names' => 'required',
                    'truename' => 'required',
                    'username' => 'required',
                    'password' => 'required',
                    'c_password' => 'required|same:password',
                ];
            }
            case 'manage':
            {
                return [
                    'role_names' => 'sometimes|required',
                    'truename' => 'sometimes|required',
                    'username' => 'sometimes|required',
                    'password' => 'nullable|same:c_password',
                    'c_password' => 'nullable|same:password',
                    'phone' => 'sometimes|required|regex:/^1[345789][0-9]{9}$/',
                ];
            }
            default:
            {
                return $rules;
            };
        }


    }

    public function messages()
    {
        return [
            'c_password.required' => '请再次输入密码',
            'c_password.same' => '两次密码输入不一样',
            'oldpassword.required' => '请填写原密码',
            'password.same' => '两次密码输入不一样',
            'phone.regex' => '手机号码格式不正确',
            'phone.required' => '请输入手机号码',
            'password.required' => '请填写密码',
            'username.required' => '用户名不能为空',
            'truename.required' => '姓名不能为空',
            'roles.required' => '用户角色不能为空',
        ];
    }

}






